<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="visible"
    :width="screenWidth"
    :fullscreen="mobile === 'mobile'"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog"
  >
    <el-form ref="addOrEditFormRef" v-loading="loading" :model="formData" :rules="rules" label-width="90" size="">
      <el-row justify="start" class="" style="margin-left: 0px">
        <el-col :span="itemSpan" class="">
          <el-form-item label="进场人员" prop="memberId">
            <el-input v-model="formData.personName" readonly placeholder="请选择进场人员" />
            <el-button
              v-if="mobile !== 'mobile'"
              class="input_append_btn_345"
              type="primary"
              plain
              @click="openPersonSelect"
              ><el-icon><User /></el-icon>&nbsp;选择</el-button
            >
          </el-form-item>
        </el-col>

        <el-col v-if="mobile === 'mobile'" :span="itemSpan">
          <el-form-item label="" prop="">
            <el-button type="primary" plain @click="openPersonSelect"
              ><el-icon><User /></el-icon>&nbsp;选择</el-button
            >
          </el-form-item>
        </el-col>

        <el-col :span="itemSpan" class="">
          <el-form-item label="考勤日期" prop="checkinDate">
            <el-date-picker
              v-model="formData.checkinDate"
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              :placeholder="dialogAddOrEdit.checkinDate === '详情' ? '' : '请选择日期'"
            />
          </el-form-item>
        </el-col>

        <el-col :span="itemSpan" class="">
          <el-form-item label="进出标识" prop="entryExitMark">
            <el-select
              v-model="formData.entryExitMark"
              :placeholder="dialogAddOrEdit.detail === '详情' ? '' : '请选择进出标识'"
              filterable
              clearable
            >
              <el-option v-for="item in EntryExitMark" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <SelectAllMember
      v-if="dialogSelectPerson.visible"
      :dialog-select-person="dialogSelectPerson"
      @personSendClose="personSendClose"
    ></SelectAllMember>
    <template #footer>
      <div class="dialog_footer">
        <el-button
          v-if="dialogAddOrEdit.detail !== '详情' && currentSystem !== 'corp' && currentSystem !== 'constructor'"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >{{ dialogAddOrEdit.footBtn }}</el-button
        >
        <el-button @click="closeDialog()">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, nextTick, computed } from "vue";
import { checkInDetailEditForm } from "@/api/admin/project/types";
import { getDicts } from "@/store/modules/dict";
import SelectAllMember from "@/views/common/SelectAllMember/index.vue";

import { useScreen } from "@/store/modules/screen";

const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenWidth = ref(900);
const itemSpan = ref(12);

function initScreen() {
  if (mobile.value === "mobile") {
    screenWidth.value = screenStore.screenWidth;
    itemSpan.value = 24;
  } else {
    screenWidth.value = 900;
    itemSpan.value = 12;
  }
}

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "新增考勤信息",
      footBtn: "申请",
      id: 0,
    }),
  },
  currentSystem: {
    type: String,
    default: () => "manager",
  },
  addAttendance: {
    type: Function,
    default: () => null,
  },
  editAttendance: {
    type: Function,
    default: () => null,
  },
  getAttendanceInfo: {
    type: Function,
    default: () => null,
  },
});

const getAttendanceInfo = props.getAttendanceInfo;
const editAttendance = props.editAttendance;
const addAttendance = props.addAttendance;

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
let visible = ref(props.dialogAddOrEdit.visible);
const formData = reactive<checkInDetailEditForm>({});
const addOrEditFormRef = ref(ElForm);
const dialogSelectPerson = reactive<DialogOption>({
  visible: false,
  title: "选择人员",
  id: 0,
});

const rules = reactive<FormRules<checkInDetailEditForm>>({
  memberId: [{ required: true, message: "请选择进场人员", trigger: "blur" }],
  checkinDate: [{ required: true, message: "请选择考勤日期", trigger: "blur" }],
  entryExitMark: [{ required: true, message: "请选择标识", trigger: "blur" }],
});

onMounted(async () => {
  initScreen();

  let tempFormData;
  if (props.dialogAddOrEdit.id) {
    loading.value = true;
    tempFormData = await getAttendanceInfo(props.dialogAddOrEdit.id);
    tempFormData = tempFormData.data;
    Object.assign(formData, tempFormData);
    loading.value = false;
    tempFormData = null;
  }
});

/* 发送当前弹窗关闭 */
function sendClose(query) {
  emit("sendClose", query);
}

/**
 * 关闭弹窗
 */
function closeDialog(query) {
  visible.value = false;
  sendClose(query);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();

  formData.id = undefined;
}

/* 关闭人员选择 */
function personSendClose(personInfo) {
  if (personInfo) {
    formData.personName = personInfo.personName;
    formData.memberId = personInfo.id;
  }
  dialogSelectPerson.visible = false;
}

/* 打开人员选择 */
function openPersonSelect() {
  // dialogSelectPerson.memberId = formData.memberId;
  dialogSelectPerson.visible = true;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        editAttendance(id, formData)
          .then((resp) => {
            if (resp.success === true) {
              ElMessage.success(resp.msg);
              closeDialog(true);
            } else {
              ElMessage.warning(resp.msg);
            }
          })
          .catch((e) => {
            console.log(e);
          })
          .finally(() => (loading.value = false));
      } else {
        addAttendance(formData)
          .then((resp) => {
            if (resp.success === true) {
              ElMessage.success(resp.msg);
              closeDialog(true);
            } else {
              ElMessage.warning(resp.msg);
            }
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}
const BankTypeTypes = getDicts("bankTypeTypes"); //银行类别
const EntryExitMark = getDicts("entryExitMark"); // 进出标识
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 60px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
